/**
 * SlotDuino
 * 
 * Copyright (c) 2011/2012 Thiago Hummel Moreira, Ricardo Mizuuti, Rafael Mufato, Bruno Sakai, Andre Turina
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
using System.IO;
using System.Text;
using Microsoft.SPOT;

namespace SlotDaemon
{
    public class TrackConfig
    {
        public string Name { get; set; }
        /// <summary>
        /// Track length in centimeters
        /// </summary>
        public int Length { get; set; }
        public byte LaneCount { get; set; }
        public byte VoltageCount { get; set; }


        #region Constructors
        public TrackConfig()
        {
        }

        public TrackConfig(string name, int length, byte laneCount, byte voltageCount)
        {
            Name = name;
            Length = length;
            LaneCount = laneCount;
            VoltageCount = voltageCount;
        }
        #endregion

        public static TrackConfig LoadConfig()
        {
            TrackConfig trackConfig;

            if (File.Exists(@"\SD\TrackConfig.dat"))
            {
                string content;
                using (var fs = new FileStream(@"\SD\TrackConfig.dat", FileMode.Open))
                {
                    using (StreamReader reader = new StreamReader(fs))
                    {
                        content = reader.ReadLine();
                    }
                }

                trackConfig = Deserialize(content);
            }
            else
            {
#if DEBUG
                Debug.Print("File not found");
#endif
                trackConfig = new TrackConfig("Unnamed Track", 0, 0, 0);
            }

            return trackConfig;
        }


        public bool WriteConfig()
        {
            if (!Directory.Exists(@"\SD")) return false;

            string trackConfig = this.Serialize();
            using (var fs = new FileStream(@"\SD\TrackConfig.dat", FileMode.Create))
            {
                using (StreamWriter streamWriter = new StreamWriter(fs))
                {
                    streamWriter.WriteLine(trackConfig);
                }
            }
            return true;
        }

        public static TrackConfig Deserialize(string data)
        {
            string[] fields = data.Split(';');
            TrackConfig obj = new TrackConfig();
            obj.Name = fields[0];
            obj.Length = int.Parse(fields[1]);
            obj.LaneCount = byte.Parse(fields[2]);
            obj.VoltageCount = byte.Parse(fields[3]);
            return obj;
        }

        public string Serialize()
        {
            StringBuilder data = new StringBuilder();
            data.Append(Name);
            data.Append(';');
            data.Append(Length);
            data.Append(';');
            data.Append(LaneCount);
            data.Append(';');
            data.Append(VoltageCount);
            return data.ToString();
        }

    }        
}

